package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

/**
 * @author 韩先楚
 */
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper  orderMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 营业额统计
     **/
    public TurnoverReportVO turnoverReport(LocalDate start, LocalDate end) {
        ArrayList<LocalDate> arrayList = new ArrayList();
        arrayList.add(start);
        while (! start.equals(end))
        {
            start = start.plusDays(1);
            arrayList.add(start);
        }

        ArrayList<Double> list = new ArrayList();
        for (LocalDate date : arrayList)
        {
          LocalDateTime starttime = LocalDateTime.of(date, LocalTime.MIN);
          LocalDateTime endtime = LocalDateTime.of(date, LocalTime.MAX);
            HashMap map = new HashMap();
            map.put("begin",starttime);
            map.put("end",endtime);
            map.put("status", Orders.COMPLETED);
         Double turnover  =  orderMapper.sumByMap(map);
         turnover = turnover == null? 0.0 : turnover;
          list.add(turnover);
        }


     return TurnoverReportVO.builder()
             .dateList(StringUtils.join(arrayList,","))
             .turnoverList(StringUtils.join(list,","))
             .build();
    }

    /**
     * 用户数量统计
     */
    public UserReportVO userReport(LocalDate start, LocalDate end) {
        ArrayList<LocalDate> arrayList = new ArrayList();
        arrayList.add(start);
        while (! start.equals(end))
        {
            start = start.plusDays(1);
            arrayList.add(start);
        }
        //统计用户总量
        ArrayList<Integer> userlist = new ArrayList<>();
        for (LocalDate date : arrayList)
        {
            LocalDateTime date1 = LocalDateTime.of(date, LocalTime.MAX);
          Integer count =  userMapper.countByDate(date1);
          if (count == null)
              count = 0;
          userlist.add(count);
        }
        //统计新增人数
        ArrayList<Integer> newuserlist = new ArrayList<>(arrayList.size());
        //先查询比第一天前一天是总数量
        LocalDate pussdate = start.plusDays(-1);
        LocalDateTime time = LocalDateTime.of(pussdate, LocalTime.MAX);
        Integer i = userMapper.countByDate(time);
        if (i == null)
            i = 0;

          //统计各个日期新增人数
        if (userlist.get(0) - i > 0)
            newuserlist.add(userlist.get(0) - i);
        else
            newuserlist.add(0);
        for (int j=1 ; j< arrayList.size();j++)
        {
            if (userlist.get(j) - userlist.get(j-1) > 0)
                newuserlist.add(userlist.get(j) - userlist.get(j-1));
            else
                newuserlist.add(0);
        }

        return UserReportVO.builder()
                .dateList(StringUtils.join(arrayList,","))
                .totalUserList(StringUtils.join(userlist,","))
                .newUserList(StringUtils.join(newuserlist,","))
                .build();
    }

    /**
     * 订单数量统计
     */
    public OrderReportVO orderReport(LocalDate begin, LocalDate end) {
        //日期列表
        ArrayList<LocalDate> dateList = new ArrayList();
        dateList.add(begin);
        while (! begin.equals(end))
        {
            begin =  begin.plusDays(1);
            dateList.add(begin);
        }
        //获取每个日期的总订单和有效订单数量
        List<Integer> totalList = new ArrayList<>();
        List<Integer> validList = new ArrayList<>();
        for (LocalDate date : dateList) {
            Integer i = orderMapper.countByMap(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX), null);
            totalList.add(i == null? 0 : i);
            Integer i1 = orderMapper.countByMap(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX), Orders.COMPLETED);
            validList.add(i1 == null? 0 : i1);
        }
        //获取总订单数和总用效订单数
        int sum = totalList.stream().mapToInt(Integer::intValue).sum();
        int sum1 = validList.stream().mapToInt(Integer::intValue).sum();
        //有效订单率
        double validRate;
        if (sum == 0)
            validRate = 0.0;
        else
            validRate =  (sum1*1.0) / sum;

        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .orderCountList(StringUtils.join(totalList,","))
                .validOrderCountList(StringUtils.join(validList,","))
                .totalOrderCount(sum)
                .validOrderCount(sum1)
                .orderCompletionRate(validRate)
                .build();
    }
}
